package ar.org.cfired.creditos.servlets;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

public class MailServlet2 extends HttpServlet {
	private static final long serialVersionUID = 1L;

    private static final String SMTP_HOST_NAME = "cl-mail";//"smtp.fibertel.com.ar";
    private static final int SMTP_HOST_PORT = 25;
    private static final String SMTP_AUTH_USER = "envio";
    private static final String SMTP_AUTH_PWD  = "123456.e";
    
    public void init() throws ServletException {
        // Initialization here...
    }

    public void service(HttpServletRequest req, HttpServletResponse res)
            throws ServletException {
        String salida = "";
        String usuario = "";
        try {


            HttpSession sessionH = req.getSession(true);
            String to = req.getParameter("to");
/*            
            Properties props = new Properties();

            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.host", SMTP_HOST_NAME);
           // props.put("mail.smtp.auth", "false");
            props.put("mail.from", "envio@cfired.org.ar");//"rogerdipasquale@fibertel.com.ar");
            // props.put("mail.smtps.quitwait", "false");

            Session mailSession = Session.getDefaultInstance(props);
            mailSession.setDebug(true);
            Transport transport = mailSession.getTransport();

            MimeMessage message = new MimeMessage(mailSession);
            message.setSubject("Testing SMTP-SSL");
            message.setContent("This is a test", "text/plain");

            message.addRecipient(Message.RecipientType.TO,
                 new InternetAddress( to ));

           transport.connect (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
    transport.send(message);
           transport.sendMessage(message,
                message.getRecipients(Message.RecipientType.TO));
           
           */
            Properties props = new Properties();
	        props.put("mail.smtp.host", "cl-mail");
	        props.put("mail.debug", "true");
	 
	        Session session = Session.getInstance(props);
	 

	            Message msg = new MimeMessage(session);
	            msg.setFrom(new InternetAddress("envio@cfired.org.ar"));
	            InternetAddress[] address = {new InternetAddress(to)};
	            msg.setRecipients(Message.RecipientType.TO, address);
	            msg.setSubject("Prueba DProcessCreditos -- ");
	            //msg.setSentDate(new Date());
	            msg.setText("Cuerpo del mensaje");
	 
	            Transport.send(msg);

           
                salida = "<html><body> Enviado!";
                //salida += fieldName + "|" + fileName + "|" + contentType + "|" + isInMemory + "|" + sizeInBytes + "<br>";
                
                salida += "</body></html>";


            res.setContentType("text/html; charset=iso-8859-1");
            PrintWriter out = res.getWriter();

            out.print(salida);
   
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }

    public void destroy() {
        // Cleanup here...
    }

}

